package com.smmisia.gateway.config;


import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;

/**
 * Sa-Token 权限认证配置
 * @author smmisia
 * @date 2023/7/27
 */
@Configuration
public class SaTokenConfigure {
    // 注册 Sa-Token全局过滤器
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 拦截地址
                .addInclude("/**")    /* 拦截全部path */
                // 开放地址
                .addExclude("/favicon.ico")
                .addExclude("/doc.html")
                .addExclude("/webjars/**")
                .addExclude("/auth/v3/**")
                .addExclude("/shop/v3/**")
                .addExclude("/article/v3/**")
                .addExclude("/accompany/v3/**")
                .addExclude("/v3/**")
                .addExclude("/swagger-ui.html")
                .addExclude("/swagger-resources/**")
                .addExclude("/article/articleList")
                .addExclude("/article/articleNotes")
                .addExclude("/article/articleStrategy")
                // 鉴权方法：每次访问进入
                .setAuth(obj -> {
                    SaRouter.match("/**", "/auth/user/**", () -> StpUtil.checkLogin());
                })
                // 异常处理方法：每次setAuth函数出现异常时进入
                .setError(e -> {
                    return SaResult.error(e.getMessage());
                })
                ;
    }
}
